/**
 * 百分比字符串输出
 * @param num
 */
function asPercentString(num) {
    if(num && !isNaN(num)){
        if(num >= 1){
            return "100%";
        } else if (num <= -1){
            return "-100%";
        } else {
            return "" + (Math.round(num * 100)) + "%";
        }
    } else if (num == 0){
        return "0%";
    } else{
        return "-";
    }
}

function completeRegion(name) {
    if(name.indexOf('白沙')>=0){
        return '白沙黎族自治县'
    }else if(name.indexOf('琼中')>=0){
        return '琼中黎族苗族自治县'
    }else if(name.indexOf('保亭')>=0){
        return '保亭黎族苗族自治县'
    }else if(name.indexOf('乐东')>=0){
        return '乐东黎族自治县'
    }else if(name.indexOf('昌江')>=0){
        return '昌江黎族自治县'
    }else if(name.indexOf('陵水')>=0){
        return '陵水黎族自治县'
    }else{
        return name;
    }
}

/**
 * 百分比字符串输出
 * @param num
 */
function asTenThousandString(num) {
    if(num && !isNaN(num)){
        return Math.round(num / 100) / 100;
    } else {
        return "-";
    }
}

/**
 * 保留两位小数
 * @param num
 */
function numFormat(num, len) {
    if(num && !isNaN(num)){
        if(isNaN(len)){
            len = 2;
        }
        //return Math.round(num * 100) / 100;
        return Number(num).toFixed(len);
    } else if(Number(num) == 0){
        return 0;
    } else {
        return "-";
    }
}

/**
 * 截取list
 */
function subListFromLast(list, length) {
    var result = {
        xData: [],
        yData: []
    };;
    if(list){
        if(list.xData.length <= length){
            result = list;
        } else {
            for(var i = 0; i < length; i ++){
                result.xData.push(list.xData[list.xData.length - 6 + i]);
                result.yData.push(list.yData[list.yData.length - 6 + i]);
            }
        }
    }
    return result;
}

//写cookies
function setCookie(name,value)
{
    /*var Days = 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();*/
    $.cookie(name, value, { expires: 1});
}
//读cookies
function getCookie(name)
{
    /*var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;*/
    return $.cookie(name);
}
//删cookies
function delCookie(name)
{
    /*var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null)
        document.cookie= name + "="+cval+";expires="+exp.toGMTString();*/
    $.removeCookie(name);
}

/**
 * 判断同比环比上升还是下降
 * @param name
 */
function upOrDown(num) {
    if (isNaN(num)){
        return "";
    } else {
        if(Number(num) > 0){
            return "rate asc"
        } else if(Number(num) < 0){
            return "rate desc";
        } else {
            return "rate";
        }
    }
}

/**
 * 去化周期显示
 * @param num
 */
function digestCycleProcessShow(num) {
    if(isNaN(num)){
        return "";
    } else {
        //cell6
        var showNum = Number(num).toFixed(0);
        if(showNum < 1){
            showNum = 1;
        } else if (showNum > 6){
            showNum = 6;
        }
        return "cell" + showNum;
    }
}

/**
 * 月份减去num个月，返回日期字符串：yyyy-MM
 * @param dateStr yyyy-MM
 * @param num 多少个月
 */
function minusMonth(dateStr, num) {
    var reg = /^\d{4}-\d{1,2}$/;
    num --;
    if(dateStr && dateStr.match(reg) != null){
        var strArr = dateStr.split('-');
        var monthResult = Number(strArr[1]) - num;
        if(monthResult >= 1){
            return strArr[0] + '-' + monthResult;
        } else {
            return Number(strArr[0]) - 1 + '-' + (12 + Number(monthResult));
        }
    }
};


function getMaxMin(arr,maxmin){
  if(maxmin === "max"){
    return Math.max.apply(Math,arr);
  }else if(maxmin === "min"){
    return Math.min.apply(Math,arr);
  }
}

/**
 * 数组最小值
 * @param Array
 * 
 */
Array.prototype.min = function(){
	  var min = this[0];
	  var len = this.length;
	  for(var i=1; i<len; i++){
	    if(this[i] < min){
	      min = this[i];
	    }
	  }
	  return min;
	}
/**
 * 数组最大值
 * @param Array 
 *
 */
	Array.prototype.max = function(){
	  var max = this[0];
	  var len = this.length;
	  for(var i=1; i<len; i++){
	    if(this[i] > max){
	      max = this[i];
	    }
	  }
	  return max;
	}